<?php

namespace backend\controllers;

use common\models\Category;
use Yii;
use common\models\document;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\components\Upload;

/**
 * ArticleController implements the CRUD actions for document model.
 */
class ArticleController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all document models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => document::find()->orderBy('id DESC'),
        ]);
		Yii::$app->view->params['titles'] = '文章列表';
        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Creates a new document model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new document();

        if ($model->load(Yii::$app->request->post())) {
			/* 处理上传的图片 */
			if ($_FILES['Document']['error']['image'] === 0) {
				/* 把文件的路劲赋值给image字段 */
				$model->image = Upload::uploadImage($model, 'image', 'other');
			}

			if ($model->save()) {
				Yii::$app->session->setFlash('success', '新增成功');
			}
            return $this->redirect(['index']);
        } else {
			$list = Category::find()->select(['id', 'name'])->where(['status' => 1])->all();
			Yii::$app->view->params['titles'] = '新增文章';
            return $this->render('create', [
                'model' => $model,
				'list' => $list
            ]);
        }
    }

    /**
     * Updates an existing document model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
			/* 处理上传的图片 */
			if ($_FILES['Document']['error']['image'] === 0) {
				/* 把文件的路劲赋值给image字段 */
				$model->image = Upload::uploadImage($model, 'image', 'other');
			}

			if ($model->save()) {
				Yii::$app->session->setFlash('success', '更新成功');
			}
            return $this->redirect(['index']);
        } else {
			$list = Category::find()->select(['id', 'name'])->where(['status' => 1])->all();

			Yii::$app->view->params['titles'] = '编辑文章';
            return $this->render('update', [
                'model' => $model,
				'list' => $list
            ]);
        }
    }

    /**
     * Deletes an existing document model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the document model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return document the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = document::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
